package com.lineage.commons.system;

import com.lineage.config.ConfigIpCheck;
import com.lineage.server.datatables.lock.IpReading;
import com.lineage.server.thread.GeneralThreadPool;
import java.io.IOException;
import java.security.Permission;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class LanSecurityManager extends SecurityManager {
  private static final Log _log = LogFactory.getLog(LanSecurityManager.class);
  
  public static final Map<String, Integer> BANIPPACK = new ConcurrentHashMap<>();
  
  public static final Map<String, Integer> BANIPMAP = new HashMap<>();
  
  public static final Map<String, Integer> BANNAMEMAP = new HashMap<>();
  
  public static final Map<String, Integer> ONEIPMAP = new ConcurrentHashMap<>();
  
  public static final Map<String, Long> ONETIMEMILLISMAP = new ConcurrentHashMap<>();
  
  public static final Map<String, Integer> Loginer = new HashMap<>();
  
  public void checkAccept(String host, int port) {
    IpReading.get().checktime(host);
    if (ConfigIpCheck.IPCHECKPACK) {
      if (BANIPMAP.containsKey(host))
        throw new SecurityException(); 
      if (BANIPPACK.containsKey(host))
        throw new SecurityException(); 
    } else {
      if (BANIPMAP.containsKey(host))
        throw new SecurityException(); 
      if (ONEIPMAP.containsKey(host))
        throw new SecurityException(); 
      if (ONETIMEMILLISMAP.containsKey(host))
        throw new SecurityException(); 
      if (ConfigIpCheck.ONETIMEMILLIS != 0)
        ONETIMEMILLISMAP.put(host, Long.valueOf(System.currentTimeMillis())); 
      if (ConfigIpCheck.ISONEIP)
        ONEIPMAP.put(host, Integer.valueOf(port)); 
      if (ConfigIpCheck.IPCHECK && !IpAttackCheck.get().check(host))
        throw new SecurityException(); 
    } 
  }
  
  public void checkAccess(Thread t) {}
  
  public void checkPermission(Permission perm) {}
  
  public void stsrt_cmd() throws IOException {
    RemoveIp removeIp = new RemoveIp(ConfigIpCheck.ONETIMEMILLIS);
    GeneralThreadPool.get().execute(removeIp);
  }
  
  public void stsrt_cmd_tmp() throws IOException {
    RemoveTmpIp removeIp = new RemoveTmpIp();
    GeneralThreadPool.get().execute(removeIp);
  }
  
  private class RemoveTmpIp implements Runnable {
    private RemoveTmpIp() {}
    
    public void run() {
      try {
        while (true) {
          Thread.sleep(1000L);
          if (!LanSecurityManager.BANIPPACK.isEmpty()) {
            Iterator<String> iterator = LanSecurityManager.BANIPPACK.keySet().iterator();
            while (iterator.hasNext()) {
              String ip = iterator.next();
              int time = ((Integer)LanSecurityManager.BANIPPACK.get(ip)).intValue() - 1;
              if (time <= 0) {
                LanSecurityManager.BANIPPACK.remove(ip);
                continue;
              } 
              LanSecurityManager.BANIPPACK.put(ip, Integer.valueOf(time));
            } 
          } 
        } 
      } catch (Exception e) {
        LanSecurityManager._log.error(e.getLocalizedMessage(), e);
        return;
      } 
    }
  }
  
  private class RemoveIp implements Runnable {
    public int _time;
    
    public RemoveIp(int oNETIMEMILLIS) {
      this._time = 60000;
      this._time = oNETIMEMILLIS;
    }
    
    public void run() {
      try {
        while (true) {
          Thread.sleep(10000L);
          if (!LanSecurityManager.ONETIMEMILLISMAP.isEmpty()) {
            Iterator<String> iterator = LanSecurityManager.ONETIMEMILLISMAP.keySet().iterator();
            while (iterator.hasNext()) {
              String ip = iterator.next();
              long time = ((Long)LanSecurityManager.ONETIMEMILLISMAP.get(ip)).longValue();
              if (System.currentTimeMillis() - time >= this._time)
                LanSecurityManager.ONETIMEMILLISMAP.remove(ip); 
            } 
          } 
        } 
      } catch (Exception e) {
        LanSecurityManager._log.error(e.getLocalizedMessage(), e);
        return;
      } 
    }
  }
}
